package csu.web.credit_bank.controller;

import csu.web.credit_bank.pojo.PostComments;

import csu.web.credit_bank.pojo.dto.PostCreateDto;
import csu.web.credit_bank.pojo.dto.CommentCreateDto;

import csu.web.credit_bank.service.PostCommentsService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comments")
@CrossOrigin
public class GetPostCommentsController {
    private final PostCommentsService postCommentsService;

    public GetPostCommentsController(PostCommentsService postCommentsService) {
        this.postCommentsService = postCommentsService;
    }

    @PostMapping("")
    public ResponseEntity<Map<String, Object>> createPostComment(@RequestBody CommentCreateDto commentCreateDto){
        String uid = postCommentsService.createComment(commentCreateDto).getPostId();
        Map<String, Object> responseBody = new HashMap<>();
        responseBody.put("code", 200);
        responseBody.put("message", "评论发布成功");
        responseBody.put("data", uid);
        return ResponseEntity.ok(responseBody);
    }
    @GetMapping("{postId}")
    public ResponseEntity<List<PostComments>> getPostCommentsList(@PathVariable String postId){
        List<PostComments> postCommentsList = postCommentsService.getCommentsByPostId(postId);
        return ResponseEntity.ok(postCommentsList);
    }
    @GetMapping("/article/{articleId}")
    public ResponseEntity<List<PostComments>> getPostCommentsListByArticleId(@PathVariable String articleId){
        List<PostComments> postCommentsList = postCommentsService.getCommentsByArticleId(articleId);
        return ResponseEntity.ok(postCommentsList);
    }
}
